<?php

namespace app\server;


use think\facade\Config;

class HredisServer
{
    //当前对象
    private $redis;
    //数据库对象
    private $db;
    protected $config;
    private $error = '';

    /**
     * Hredis constructor.
     * @param int $db
     */
    public function __construct($db = 0)
    {
        $this->config = config('database.redis_config');
        $this->db = $db;
    }

    /**
     * @function redis操作
     * @param $func
     * @param $args
     * @return bool|mixed
     */
    public function __call($func, $args)
    {
        if ($this->config['multi']) {
            switch ($func) {
                case 'set':
                    $isMaster = true;
                    break;
                case 'setbit':
                    $isMaster = true;
                    break;
                case 'incr':
                    $isMaster = true;
                    break;
                case 'setex':
                    $isMaster = true;
                    break;
                case 'zadd':
                    $isMaster = true;
                    break;
                case 'zDelete':
                    $isMaster = true;
                    break;
                case 'zDeleteRangeByScore':
                    $isMaster = true;
                    break;
                default:
                    $isMaster = false;
                    break;
            }
        } else {
            $isMaster = true;
        }
        if ($this->setup($isMaster)) {
            return call_user_func_array(array($this->redis,$func),$args);
        } else {//连接redis失败
            /*$smsObj = new Sms('');
            $param = ['point' => '连接redis失败', 'message' => $this->error];
            $smsObj->sendSystemMsg($param);*/
            return false;
        }
    }

    /**
     *@function 销毁对象
     *
     */
    public function __destruct()
    {
        if ($this->redis) {
            $this->redis->close();
        }
    }

    /**
     * @function 初始化连接redis
     * @param bool $isMaster
     * @return bool
     */
    private function setup($isMaster = true)
    {
        if (!$this->config['multi'] || $isMaster) {//没有读写分离
            //或者有读写分离，写操作
            $config = $this->config['master'];
        } else {
            //读操作
            $config = $this->config['slave'];
        }

        return $this->connected($config);
    }

    /**
     * @function 连接redis
     * @param array $config
     * @return bool
     */
    private function connected($config = [])
    {
        $this->redis = new \Redis();

        try {
            if ($config['socket_type'] === 'unix') {//如果是unix
                $success = $this->redis->connect($config['socket']);
            } else {
                $success = $this->redis->connect($config['host'], $config['port'], $config['timeout']);
            }

            if (!$success) {
                $this->error = 'redis connect fail,not redis server or other';
                return false;
            }
        } catch (\RedisException $e) {
            $this->error = $e->getMessage();
            return false;
        }
        if (!empty($config['password'])) {
            $isAuth = $this->redis->auth($config['password']);
            if (!$isAuth) {
                $this->error = 'redis auth fail,password not right';
                return false;
            }
        }

        $this->redis->select($this->db);
        return true;
    }


}
